﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Reports.Ticket
{
    public class Report2 : DependencyObject
    {
        public static DependencyProperty StartDateProperty = DependencyProperty.Register("StartDate", typeof(DateTime), typeof(Report2));
        public DateTime StartDate
        {
            get { return (DateTime)GetValue(StartDateProperty); }
            set
            {
                SetValue(StartDateProperty, value);
            }
        }
        public static DependencyProperty EndDateProperty = DependencyProperty.Register("EndDate", typeof(DateTime), typeof(Report2));
        public DateTime EndDate
        {
            get { return (DateTime)GetValue(EndDateProperty); }
            set
            {
                SetValue(EndDateProperty, value);
            }
        }
        public static DependencyProperty TopProperty = DependencyProperty.Register("Top", typeof(int), typeof(Report2));
        public int Top
        {
            get { return (int)GetValue(TopProperty); }
            set
            {
                SetValue(TopProperty, value);
            }
        }
        public Report2()
        {
            EndDate = DateTime.Now;
            if (DateTime.Now.Month > 1)
                StartDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month - 1, DateTime.Now.Day);
            else StartDate = new DateTime(DateTime.Now.Year - 1, 12, DateTime.Now.Day);
        }
        public CrystalReport2 Load(TinyCinema.Bussiness.Login.Employee LoginInfo)
        {
            string sql = " set dateformat mdy"
                        + " select PHIM.TenPhim,LOAISUATCHIEU.TenLSC,table2.Sove,table2.DoanhSo1 from"
                        + " ("
                        + " select top " + Top
                        + " PHIM.MaPhim,SUM(Ve.Gia) as DoanhSo"
                        + " from PHIM,VE,SUATCHIEU,LOAISUATCHIEU,DONGIASUATCHIEU"
                        + " where PHIM.MaPhim = SUATCHIEU.MaPhim"
                        + " and VE.MaSC=SUATCHIEU.MaSC"
                        + " and SUATCHIEU.MaDG = DONGIASUATCHIEU.MaDG"
                        + " and SUATCHIEU.NgayChieu>='" + StartDate + "'"
                        + " and SUATCHIEU.NgayChieu<='" + EndDate + "'"
                        + " and DONGIASUATCHIEU.MaLSC = LOAISUATCHIEU.MaLSC"
                        + " group by PHIM.MaPhim"
                        + " ) as table1,"
                        + " ("
                        + " select PHIM.MaPhim,LOAISUATCHIEU.MaLSC,COUNT(VE.MaVe) as Sove,SUM(Ve.Gia) as DoanhSo1"
                        + " from PHIM,VE,SUATCHIEU,LOAISUATCHIEU,DONGIASUATCHIEU"
                        + " where PHIM.MaPhim = SUATCHIEU.MaPhim"
                        + " and VE.MaSC=SUATCHIEU.MaSC"
                        + " and SUATCHIEU.MaDG = DONGIASUATCHIEU.MaDG"
                        + " and DONGIASUATCHIEU.MaLSC = LOAISUATCHIEU.MaLSC"
                        + " group by PHIM.MaPhim,LOAISUATCHIEU.MaLSC"
                        + " ) as table2, PHIM, LOAISUATCHIEU"
                        + " where table1.MaPhim = table2.MaPhim"
                        + " and Phim.MaPhim = table1.MaPhim"
                        + " and LOAISUATCHIEU.MaLSC = table2.MaLSC"
                        + " order by table1.DoanhSo desc";         
            CrystalReport2 report = new CrystalReport2();
            report.SetDataSource(TinyCinema.Data.Execution.Excute(sql));
            report.SetParameterValue("StartDate", StartDate);
            report.SetParameterValue("EndDate", EndDate);
            report.SetParameterValue("EmployeeName", LoginInfo.Emp.Name);
            report.SetParameterValue("Top", Top);
            return report;
        }
    }
}
